class Solution(object):
    def removeKdigits(self, num, k):
        """
        :type num: str
        :type k: int
        :rtype: str
        """
        if k == len(num):
            return "0"
        elif k == len(num) - 1:
            return min(num)

        div_num = num
        candi_num = []
        i, step = 0, 0
        while True:
            if  i < len(div_num) - 1 and div_num[i] > div_num[i+1]:
                div_num = div_num[:i] + div_num[i+1:]
                i = i - 1 if not i == 0 else 0 
                step += 1
                if step == k:
                    break
            elif i == len(div_num) - 1:

                div_num = div_num[:(step - k)]
                break
            else:
                i += 1

        return str(int(div_num))



so = Solution()
num = "33526221184202197273"
k = 19
print(so.removeKdigits(num,k))